using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Text;
using Random = System.Random;

public class ErrorColor : MonoBehaviour
{
    private string stringValue;
    public InputField inputField;
    private string inputText;
    private string fileString;

    //public Text errorText;

    public Button submitButton;

    private AudioSource victorSource;
    //public AudioClip victorClip;

    public GameObject overPanel;
    public Text overGameText;
    public Image image;
    //private int index = 0;
    
    private  Color differentColor=Color.red;
    private  Color sameColor=Color.white;
    public Text compareText;
    private string compareString;

    private int number=1;
    private RandomText rt;
    public int fileNumber = 66;

    
    private void Start()
    {
        
        inputText = inputField.text;

        fileString = File.ReadAllText("Assets/text/1.txt");

        Debug.Log(number);
        print("start主函数没问题");

        submitButton.onClick.AddListener(IsTrue);
        
    }

    public void ChangeString()
    {
        
        for (int i = 1; i <= fileNumber; i++)
        {
            if (RandomText.num1 == i)
            {
                fileString = File.ReadAllText("Assets/text/" + RandomText.num1.ToString() + ".txt");
            }
        }
    }

    private void Update()
    {
        ChangeString();
        inputText = inputField.text;
    }

    public void IsTrue()
    {

        AudioManage.instance.PlayButtonAudio();
        
        overPanel.SetActive(true);
        
        if (fileString == inputText)
        {
            AudioManage.instance.VictorAudio();
            overGameText.text = "全队啦~";
            
            image.enabled = true;
            print("success!!!!");
            inputField.text = "";
        }
        else
        {
            AudioManage.instance.FailAudio();
            overGameText.text = "有错误哦~";
            
            image.enabled = false;
            print("fail!!!!");
            print("即将开启协程");
            //StartCoroutine(ErrorTip());
            StartCoroutine(ErrorTip());
            inputField.interactable = true;
            print("协程关闭");
        }
    }


    //检测缺少的字以及提示
    IEnumerator ErrorTip()
    {
        
        print("开启协程");
        inputText = inputField.text;
        for (int i = 0; i < fileString.Length; i++)
        {
            if (inputText.Length <=i || fileString[i]!=inputText[i])
            {
                compareText.text = "缺少了";

                if (inputText.Length <= i)
                {
                    compareText.text = "(" + fileString.Substring(i)+")"+"~缺少啦~";
                    compareText.color=Color.white;

                }
                else
                {
                    compareText.text = "(" + inputText.Substring(i)+")";
                    compareText.color=Color.red;
                }
                break;
                
            }

            if (compareText.text == "")
            {
                print("全队啦！");
            }

            inputField.interactable = false;
        }
        print("协程结束");

        yield return null;

    }
}
